//
//  FabuViewController.m
//  HuateVideo
//
//  Created by Tiger on 12-5-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FabuViewController.h"

@interface FabuViewController ()

@end

@implementation FabuViewController
@synthesize localVideo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [barButtonItem release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.view.backgroundColor = TOOLBAR_TINTCOLOR;
    self.title = LS(@"发布");
    
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 265, 20)];
    lblTitle.text = LS(@"文件标题");
    lblTitle.font = [UIFont boldSystemFontOfSize:14.0];
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lblTitle];
    [lblTitle release];
	_tfName = [[UITextField alloc]initWithFrame:CGRectMake(25, 40, 270, 35)];
	_tfName.tag = 1001;
	_tfName.keyboardType = UIKeyboardTypeEmailAddress;
	_tfName.placeholder = LS(@"文件标题");
	_tfName.font = [UIFont systemFontOfSize:14.0];
	_tfName.borderStyle = UITextBorderStyleRoundedRect;
    _tfName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	_tfName.returnKeyType = UIReturnKeyNext;
	_tfName.clearButtonMode = UITextFieldViewModeAlways;
	_tfName.delegate = self;
	[self.view addSubview:_tfName];
	
	
	lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(30, 90, 265, 20)];
    lblTitle.text = LS(@"文件描述");
    lblTitle.font = [UIFont boldSystemFontOfSize:14.0];
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lblTitle];
    [lblTitle release];
	
	UITextField *textViewBg = [[UITextField alloc]initWithFrame:CGRectMake(25, 110, 270, 115)];
	textViewBg.borderStyle = UITextBorderStyleRoundedRect;
	textViewBg.userInteractionEnabled = NO;
	[self.view addSubview:textViewBg];
	[textViewBg release];
	
	_textView = [[TextViewWithPlaceholder alloc]initWithFrame:CGRectMake(30, 115, 260, 105)];
	_textView.font = [UIFont systemFontOfSize:15.0f];
	_textView.placeholder = LS(@"文件描述");
	_textView.delegate = self;
	[self.view addSubview:_textView];
    
    barButtonItem = [[UIBarButtonItem alloc] initWithTitle: LS(@"关闭键盘")
                                                     style:UIBarButtonItemStylePlain
                                                    target: self 
                                                    action: @selector(textViewShouldReturn)];
    
    
    UIButton *fabu = [UIButton buttonWithType:UIButtonTypeCustom];
    fabu.frame = CGRectMake(30, 285, 60, 30);
    fabu.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [fabu setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
    [fabu setTitle:LS(@"发布") forState:UIControlStateNormal];
    [fabu addTarget:self action:@selector(toFabu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fabu];
    
    if ([localVideo.fileType isEqualToString:@"V"]) {
        typeList = [[NSArray alloc]initWithObjects:LS(@"原创视频"),LS(@"电影"),LS(@"微视频"),LS(@"视频资讯"), nil];
        
        UISegmentedControl *gc = [[UISegmentedControl alloc]initWithItems:typeList];
        gc.frame = CGRectMake(25, 240, 270, 30);
        gc.tintColor = TOOLBAR_TINTCOLOR;
        
        [gc setSegmentedControlStyle:UISegmentedControlStyleBar];
        [gc setSelectedSegmentIndex:0];
        type = @"原创视频";
        [gc addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:gc];
        [gc release];
    }
    else {
        type = @"图片";
    }
}

- (void)segmentChanged:(UISegmentedControl *)sender
{
    type = [typeList objectAtIndex:sender.selectedSegmentIndex];
}

- (void)toFabu
{
    if ([_tfName.text length] && [_textView.text length]) {
        localVideo.ok = 1;
        localVideo.title = _tfName.text;
        localVideo.describe = _textView.text;
        localVideo.type = type;
        localVideo.username = [UserDefaults objectForKey:USERNAME_STORE_KEY];
        [UserDefaults restoreLocalVideo:localVideo];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:REFLASH_MY_VIDEOS_NOTIFICATION object: nil];
        
        UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"成功加入到上传列表")  
                                                            message:nil
                                                           delegate:self 								  
                                                  cancelButtonTitle:LS(@"确定")
                                                  otherButtonTitles:nil];		
        [alertview show];		
        [alertview release];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    else {
        //
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if (textField.tag == 1001)
	{
		[_textView becomeFirstResponder];
	}
    
	
	return YES;
}

- (void)textViewShouldReturn
{
	[_textView resignFirstResponder];
	
	NSTimeInterval animationDuration = 0.30f;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:animationDuration];
	self.view.frame = CGRectMake(0, 0, 320, 416);
	[UIView commitAnimations];
    
    self.navigationItem.rightBarButtonItem = nil;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
	NSTimeInterval animationDuration = 0.30f;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:animationDuration];
	self.view.frame = CGRectMake(0, -80, 320, 416);
	[UIView commitAnimations];
    
    self.navigationItem.rightBarButtonItem = barButtonItem;
	
	return YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
